package com.cj.bus.controller;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cj.bus.common.annotation.CheckLoginApi;
import com.cj.bus.common.entity.Constents;
import com.cj.bus.common.entity.ResponseEntity;
import com.cj.bus.common.entity.ResponseHelper;
import com.cj.bus.common.entity.ValidationGroups;
import com.cj.bus.common.utils.RequestUtils;
import com.cj.bus.entity.BusResourceApplyEntity;
import com.cj.bus.entity.BusResourceCommentEntity;
import com.cj.bus.entity.BusResourceEntity;
import com.cj.bus.service.BusResourceApplyService;
import com.cj.bus.service.BusResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/resource")
@Validated
@Api(tags = {"资源相关接口"})
@Slf4j
public class BusResourceController {

	@Autowired private BusResourceService busResourceService;
	@Autowired private BusResourceApplyService busResourceApplyService;
	
	
	@SuppressWarnings({ "unchecked"})
	@GetMapping("/list")
	@ApiOperation(value = "查询所有的资源")
	public ResponseEntity<List<BusResourceEntity>> list(
			Integer page,
			String keys,
			String types,
			String orderField,
			String orderType) {
		
		PageHelper.startPage(null == page ? 1 : page, Constents.DEFAULT_ROWS);
		if(StringUtils.isBlank(orderField)) {
			PageHelper.orderBy("t.resource_id desc");
		}else {
			PageHelper.orderBy("t." + orderField + " " + null == orderType?"desc":"asc");
		}
		List<Integer> typesInt= null;
		if(StringUtils.isNotBlank(types)) {
		   typesInt= Arrays.asList(types .split(",")).stream().map(s -> (Integer.parseInt(s.trim()))).collect(Collectors.toList());
		}
		com.github.pagehelper.Page<BusResourceEntity> list = (com.github.pagehelper.Page<BusResourceEntity>)busResourceService.selectList(keys, typesInt, Constents.RESOURCE_PUB_SUCCESS_STRING);
		if(null == list || CollectionUtils.isEmpty(list.getResult())) {
			return ResponseHelper.success(new ArrayList<>());
		}
		return ResponseHelper.success(list.getResult());
	}
	
	@GetMapping("/find/{resourceId}")
	@ApiOperation(value = "查询单个资源")
	public ResponseEntity<BusResourceEntity> findOne(@PathVariable("resourceId") Long resourceId) {
		Assert.notNull(resourceId,"资源ID未输入");
		BusResourceEntity resource = this.busResourceService.getById(resourceId);
		//触发资源浏览次数+1
		busResourceService.browse(resource);
		return ResponseHelper.success(resource);
	}
	
	/*var applyList = [{
	publishUserAvatar:'/profile/avatar/2021/01/12/a6fb58df-698d-4bd8-a5bd-e02a121c02bc.png',
	applyUserName:'杨浩',
	status:'00',
	applyRemark:'1111111111111111111111111',
	applyTime:'2020-12-11 23:40:22'
}];*/
	
	@GetMapping("/find/applyList/{resourceId}")
	@ApiOperation(value = "查询资源申请列表")
	public ResponseEntity<List<BusResourceApplyEntity>> findResourceApplyList(@PathVariable("resourceId") Long resourceId) {
		Assert.notNull(resourceId,"资源ID未输入");
		List<BusResourceApplyEntity> list = busResourceApplyService.getListByResourceId(resourceId);
		return ResponseHelper.success(list);
	}
	
	@PostMapping("/push")
	@CheckLoginApi
	@ApiOperation(value = "发布资源")
	public ResponseEntity<Long> push(
			@Validated({ValidationGroups.Add.class}) 
	        @RequestBody BusResourceEntity record) {
		record.setCreateTime(LocalDateTime.now());
		if(null == record.getPublishUserId()) {
			record.setPublishUserId(RequestUtils.getLoginId());
			record.setPublishDeptId(RequestUtils.getLoginDeptId());
			record.setPublishUserName(RequestUtils.getLoginName());
		}
		record.setCommentNum(0);
		record.setStatus(Constents.RESOURCE_PUB_INIT_STRING);
		record.setStoreNum(0);
		record.setBrowseNum(0);
		this.busResourceService.save(record);
		return ResponseHelper.success(record.getResourceId());
	}
	
	@CheckLoginApi
	@PostMapping("/store")
	@ApiOperation(value = "资源收藏")
	public ResponseEntity<Long> store(
			@Validated({ValidationGroups.Update.class}) 
	        @RequestBody BusResourceEntity record) {
		BusResourceEntity busResourceEntity = this.busResourceService.getById(record.getResourceId());
		Assert.notNull(busResourceEntity, "资源ID无效！");
		this.busResourceService.store(busResourceEntity);
		return ResponseHelper.success(record.getResourceId());
	}
	
	@CheckLoginApi
	@PostMapping("/comment")
	@ApiOperation(value = "资源评论")
	public ResponseEntity<Long> comment(
			@Validated({ValidationGroups.Add.class}) 
	        @RequestBody BusResourceCommentEntity record) {
		this.busResourceService.comment(record);
		return ResponseHelper.success(record.getResourceId());
	}


}
